#coding=utf-8

from django.shortcuts import render_to_response,HttpResponseRedirect,HttpResponse
from django.template.loader import get_template
from django.template import Context
import json
from mytest.models import user_test,mssql_test,tmptest

def mytest(request):
    return render_to_response('mytest.html')

def mytest_dj_all(request):
    user = user_test.objects.order_by("-id").all()
    return render_to_response('mytest_dj.html',{'user':user})

def mytest_dj_serch(request):
    t = get_template('mytest_dj.html')
    q = ''
    user = None
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        user = user_test.objects.filter(id__icontains=q)
    else:
        user = user_test.objects.order_by("-id").all()
    html = t.render(Context({'user': user, 'search_item': q}))
    return HttpResponse(html)

def mytest_sql_all(request):
    u = user_test()
    # user = u.getuser_data()
    user = u.getuser_data_dic()
    return render_to_response('mytest_sql.html',{'user':user})

def mytest_sql_serch_01(request):
    t = get_template('mytest_sql.html')
    q = ''
    user = None
    u = user_test()
    para=[]
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        para.append(q)
        user = u.getuser_data_para(para)
    html = t.render(Context({'user': user, 'search_item': q}))
    return HttpResponse(html)

def mytest_sql_serch_02(request):
    t = get_template('mytest_sql.html')
    q = ''
    fn = ''
    user = None
    u = user_test()
    para=[]
    if 'q' in request.GET and request.GET['q'] and 'fn' in request.GET and request.GET['fn']:
        q = request.GET['q']
        fn = request.GET['fn']
        para.append(q)
        para.append(fn)
        user = u.getuser_data_para(para)
    html = t.render(Context({'user': user, 'search_item': q, 'search_item02': fn}))
    return HttpResponse(html)

def mytest_mssql_all(request):
    u = mssql_test()
    dim_date = u.get_dim_date()
    return render_to_response('mytest_mssql.html',{'dim_date':dim_date})

def mytest_echarts_map(request):
    return render_to_response('mytest_echarts_map.html')

def mytest_json_data(request):
    callback = request.GET['callback']
    u = mssql_test()
    orderamount = u.get_orderamount()
    orderamount_json = json.dumps(orderamount,ensure_ascii=False,encoding="utf-8")
    return HttpResponse('%s(%s)'%(callback,orderamount_json), content_type="application/json")

def mytest_json_data02(request):
    print request.GET
    g_data = {
        'product_type':[1,2,3,4,5],
        'orderamount':[100,212,325,214,526]
    }
    return HttpResponse(json.dumps(g_data))

def mytest_json_data03(request):
    u = tmptest()
    g_data = u.get_tmp_order_stat()
    pt=[]
    om=[]
    dic={}
    for obj in g_data:
        pt.append(obj[0])
        om.append(obj[1])

    dic['product_type']=pt
    dic['orderamount']=om
    return HttpResponse(json.dumps(dic))

def mytest_json(request):
    return render_to_response('mytest_json.html')